﻿#include <QIcon>
#include <QPixmap>
#include <QDesktopServices>
#include <QUrl>
#include "CTableWidgetB.h"
#include "CGlobal.h"
#include "cimagedownloader.h"
#include "CImgDownloadTh.h"
#include "Public.h"

CTableWidgetB::CTableWidgetB(QWidget * parent)
{
	m_nDataType = 0;//默认是任务
	connect(CImgDownloadTh::instance(), &CImgDownloadTh::imageDownloaded, this, &CTableWidgetB::onImageDownloaded);
}

CTableWidgetB::~CTableWidgetB()
{
}

void CTableWidgetB::InitUi(int nType)//初始化界面
{
	m_nDataType = nType;
	clear();
	QStringList headerText;
	setShowGrid(true);
	setSelectionMode(QAbstractItemView::SingleSelection);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setStyleSheet("QTableWidget::item{border:1px solid gray ;}");
	
	if (nType == 4)//跑偏报警记录查询
	{
		setColumnCount(9);
		headerText << A2T("序号") << A2T("点位名称") << A2T("开始时间") << A2T("结束时间")<<A2T("左侧跑偏值(mm)")<<A2T("右侧跑偏值(mm)")<<A2T("跑偏报警描述") << A2T("报警截图") << A2T("操作");
		setColumnWidth(0, 50);
		setColumnWidth(1, 180);
		setColumnWidth(2, 180);
		setColumnWidth(3, 180);
		setColumnWidth(4, 150);
		setColumnWidth(5, 150);
		setColumnWidth(6, 300);
		setColumnWidth(7, 200);
		setColumnWidth(8, 120);
	}
	if (nType == 3)//报告结果记录查询
	{
		setColumnCount(5);
		headerText << A2T("序号") << A2T("报告名称") << A2T("生成时间") << A2T("路径") << A2T("操作");
		setColumnWidth(0, 100);
		setColumnWidth(1, 280);
		setColumnWidth(2, 280);
		setColumnWidth(3, 500);
	}
	setHorizontalHeaderLabels(headerText);
	verticalHeader()->setHidden(true);
	setShowGrid(false);
	horizontalHeader()->setStretchLastSection(true);
	setSelectionBehavior(QAbstractItemView::SelectRows);
}

void CTableWidgetB::InitData(int nType, QVector<QVector<QString>>vecStr)
{
	m_sPageUuid = createGuid();
	CImgDownloadTh::instance()->clearRequest();
	QQueue<QPair<QUrl, QLabel*>> pairRequest;
	if (nType == 4)
	{
		setRowCount(vecStr.size());
		for (int i = 0; i < vecStr.size(); i++)
		{
			setRowHeight(i, 80);
			if (vecStr[i].size() < 8)
			{
				continue;
			}
			MyStructCorrectItem cMyStructItem;
			cMyStructItem.m_pLabIndex->setText(vecStr[i][0]);//序号
			cMyStructItem.m_pLabPointName->setText(vecStr[i][1]);
			cMyStructItem.m_pLabStartTime->setText(vecStr[i][2]);
			cMyStructItem.m_pLabEndTime->setText(vecStr[i][3]);
			cMyStructItem.m_pLeftValue->setText(vecStr[i][4]);
			cMyStructItem.m_pRightValue->setText(vecStr[i][5]);
			cMyStructItem.m_pAlarmDesc->setText(vecStr[i][6]);
			if (vecStr[i][7] != "")
			{
				pairRequest.enqueue(qMakePair(vecStr[i][7], cMyStructItem.m_pBtnImage));
			}
			else
			{
				cMyStructItem.m_pBtnImage->setText("/");
			}
			cMyStructItem.m_pBtnView->setProperty("HttpUrl", vecStr[i][7]);
			setCellWidget(i, 0, cMyStructItem.m_pLabIndex);
			setCellWidget(i, 1, cMyStructItem.m_pLabPointName);
			setCellWidget(i, 2, cMyStructItem.m_pLabStartTime);
			setCellWidget(i, 3, cMyStructItem.m_pLabEndTime);
			setCellWidget(i, 4, cMyStructItem.m_pLeftValue);
			setCellWidget(i, 5, cMyStructItem.m_pRightValue);
			setCellWidget(i, 6, cMyStructItem.m_pAlarmDesc);
			setCellWidget(i, 7, cMyStructItem.m_pBtnImage);
			setCellWidget(i, 8, cMyStructItem.m_pBtnView);
			connect(cMyStructItem.m_pBtnView, SIGNAL(clicked()), this, SLOT(slotClickedShowImg()));
		}
		CImgDownloadTh::instance()->addRequest(pairRequest, m_sPageUuid);
	}
	else if (nType == 3)
	{
		setRowCount(vecStr.size());
		for (int i = 0; i < vecStr.size(); i++)
		{
			setRowHeight(i, 30);
			if (vecStr[i].size() < 5)
			{
				continue;
			}
			MyStructResultItem cMyStructItem;
			cMyStructItem.m_pLabIndex->setText(vecStr[i][0]);//序号
			cMyStructItem.m_pResultName->setText(vecStr[i][1]);
			cMyStructItem.m_pReslutTime->setText(vecStr[i][2]);
			cMyStructItem.m_pPath->setText(vecStr[i][3]);
			cMyStructItem.m_pBtnView->setProperty("Path", vecStr[i][4]);
			setCellWidget(i, 0, cMyStructItem.m_pLabIndex);
			setCellWidget(i, 1, cMyStructItem.m_pResultName);
			setCellWidget(i, 2, cMyStructItem.m_pReslutTime);
			setCellWidget(i, 3, cMyStructItem.m_pPath);
			setCellWidget(i, 4, cMyStructItem.m_pBtnView);
			connect(cMyStructItem.m_pBtnView, SIGNAL(clicked()), this, SLOT(slotClickedShowResult()));
		}
	}
}

void CTableWidgetB::slotClickedShowImg()
{
	QPushButton *btn = (QPushButton *)sender();
	QString sHttpUrl = btn->property("HttpUrl").toString();//报警图片
	emit sigShowImg(sHttpUrl);
}

void CTableWidgetB::slotClickedShowResult()
{
	QPushButton *btn = (QPushButton *)sender();
	QString sPath = btn->property("Path").toString();
	emit sigShowResult(sPath);
}

void CTableWidgetB::onImageDownloaded(QLabel* pLabel, QPixmap pixmap,QString sPageUuid)
{
	if (sPageUuid == m_sPageUuid)
	{
		pLabel->setPixmap(pixmap);
		pLabel->setScaledContents(true);
		pLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
	}
}
